package com.lvyq.myplayer.adapter


import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.lvyq.myplayer.R
import com.lvyq.myplayer.common.util.MockDataUtil
import com.lvyq.myplayer.model.index.IndexVideoBean
import com.lvyq.myplayer.model.index.IndexVideoBeans
import com.squareup.picasso.Picasso
import jp.wasabeef.picasso.transformations.RoundedCornersTransformation
import org.jetbrains.anko.find


/**
 * 文件描述：IndexAdapter
 * 作者：lvyq
 * 创建时间：2025-03-24
 * 修改时间：2025-03-24
 */
class IndexVideoAdapter(var context: Context?): RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    var videoDatas:IndexVideoBean= MockDataUtil.getIndexVideoList()

    // 定义点击事件接口
    interface OnItemClickListener {
        fun onItemClick(data: IndexVideoBeans, position: Int)
    }

    private var onItemClickListener: OnItemClickListener? = null

    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.onItemClickListener = listener
    }


    fun setData( datas: IndexVideoBean){
        this.videoDatas=datas
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = View.inflate(context, R.layout.item_index_video,null)
        return indexVideoHoler(view)
    }

    override fun getItemCount(): Int {
            return  videoDatas.rows.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val viewType = getItemViewType(position)
        var data = videoDatas.rows.get(position)
        (holder as indexVideoHoler).setData(data)

        // 绑定点击事件
        holder.itemView.setOnClickListener {
            onItemClickListener?.onItemClick(data, position)
        }
    }

    /**
     * 推荐歌单
     */
    inner  class  indexVideoHoler(item:View): RecyclerView.ViewHolder(item){
        val bg:ImageView
        val timer:TextView
        val song:TextView
        init {
            bg=item.find(R.id.bg)
            timer=item.findViewById(R.id.timer)
            song=item.findViewById(R.id.song)
        }
        fun setData(data: IndexVideoBeans) {
            Picasso.with(context).load(data.img).transform(RoundedCornersTransformation(30,0)).into(bg)
            timer.text=data.timer
            song.text=data.title
        }


    }

    //定义函数类型变量
   /* var listener :((itemBean:IndexVideoBeans)->Unit)? = null
    fun setMyListener(listener: (itemBean:IndexVideoBeans)->Unit){
        this.listener=listener
    }*/
}